package com.atguigu.gulimail.product.service.impl;

import com.atguigu.gulimail.product.entity.AttrEntity;
import com.atguigu.gulimail.product.service.AttrService;
import com.atguigu.gulimail.product.service.CategoryService;
import com.atguigu.gulimail.product.vo.AttrGroupWithAttrsVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimail.product.dao.AttrGroupDao;
import com.atguigu.gulimail.product.entity.AttrGroupEntity;
import com.atguigu.gulimail.product.service.AttrGroupService;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {
    @Autowired
    AttrService attrService;
    @Autowired
    CategoryService categoryService;


    //逆向生成  带分页模糊查询
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 根据分类id 查询该分类下的属性分组 (带分页  模糊查询)
     *
     * @param params    {
     *                  page: 1,//当前页码
     *                  limit: 10,//每页记录数
     *                  sidx: 'id',//排序字段
     *                  order: 'asc/desc',//排序方式
     *                  key: '华为'//检索关键字
     *                  }
     * @param catelogId
     * @return
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params, long catelogId) {
        //前端查询的时候还会带上一个关键字key
        //key是多关键字模糊查询
        String key = (String) params.get("key");
        //select * from pms_attr_group where catelog_id=? and 检索条件--->(attr_group_id=? or  attr_group_name like key)
        //添加key中的条件查询
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<AttrGroupEntity>();
        if (!StringUtils.isEmpty(key)) {
            wrapper.and((obj) -> {
                obj.eq("attr_group_id", key).or().like("attr_group_name", key);
            });
        }
        //如果catelogId等于0，即未点击左边三级分类，就按照条件 查询所有分类的属性分组
        if (catelogId == 0) {
            //根据提供的params参数和构造的wrapper条件进行分页，返回分页好的page对象
            IPage<AttrGroupEntity> page =
                    this.page(new Query<AttrGroupEntity>().getPage(params),
                            wrapper);
            List<AttrGroupEntity> records = page.getRecords();
            //为每一个AttrGroupEntity设置catelogPath路径值
            for (int i = 0; i < records.size(); i++) {
                AttrGroupEntity entity = records.get(i);
                Long catelogId1 = entity.getCatelogId();
                Long[] catelogPath = categoryService.findCatelogPath(catelogId1);
                entity.setCatelogPath(catelogPath);
                records.set(i,entity);
            }
            page.setRecords(records);
            //返回自定义的PageUtils对象，构造方法传入已经分页好的page对象
            return new PageUtils(page);
        } else {
            wrapper.eq("catelog_id", catelogId);
            //根据提供的params参数和构造的wrapper条件进行分页，返回分页好的page对象
            IPage<AttrGroupEntity> page =
                    this.page(new Query<AttrGroupEntity>().getPage(params),
                            wrapper);
            List<AttrGroupEntity> records = page.getRecords();
            //为每一个AttrGroupEntity设置catelogPath路径值
            for (int i = 0; i < records.size(); i++) {
                AttrGroupEntity entity = records.get(i);
                Long catelogId1 = entity.getCatelogId();
                Long[] catelogPath = categoryService.findCatelogPath(catelogId1);
                entity.setCatelogPath(catelogPath);
                records.set(i,entity);
            }
            page.setRecords(records);
            //返回自定义的PageUtils对象，构造方法传入已经分页好的page对象
            return new PageUtils(page);
        }
    }

    //根据三级分类ID查出所有的分组以及这些组里面的属性
    /**
     * 获取当前分类下的所有属性分组，还要带上所有的属性
     * @param catelogId
     * @return
     */
    @Override
    public List<AttrGroupWithAttrsVo> getAttrGroupWithAttrsByCatelogId(Long catelogId) {
        //根据三级分类id 查询该三级分类的所有分组
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("catelog_id", catelogId);
        List<AttrGroupEntity> attrGroupEntities = this.list(wrapper);
        //把分组变为AttrGroupWithAttrsVo
        List<AttrGroupWithAttrsVo> collect = attrGroupEntities.stream().map(group -> {
            AttrGroupWithAttrsVo attrsVo = new AttrGroupWithAttrsVo();
            BeanUtils.copyProperties(group, attrsVo);
            //按照分组ID获取关联的所有Attr
            List<AttrEntity> attrs = attrService.getRelationAttr(attrsVo.getAttrGroupId());
            attrsVo.setAttrs(attrs);
            return attrsVo;
        }).collect(Collectors.toList());
        //查询所有属性
        return collect;
    }

}